package com.pengshun.modules.api.order.controller;

import com.fasterxml.jackson.annotation.JsonView;
import com.pengshun.config.NoRepeatSubmit;
import com.pengshun.modules.api.coal.validator.First;
import com.pengshun.modules.api.coal.validator.Fourth;
import com.pengshun.modules.api.coal.validator.Second;
import com.pengshun.modules.api.coal.validator.Third;
import com.pengshun.modules.api.order.service.OrderAllotService;
import com.pengshun.modules.api.order.service.dto.OrderAllotTransportDto;
import com.pengshun.modules.api.transport.dto.TransportDto;
import com.pengshun.modules.api.transport.dto.TransportOrderDto;
import com.pengshun.modules.common.BaseResult;
import com.pengshun.modules.common.PagePara;
import com.pengshun.modules.common.ReslutView;
import com.pengshun.modules.domain.User;
import com.pengshun.modules.security.Authorization;
import com.pengshun.modules.security.CurrUserInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

/**
 * @author luoligang
 * @create 2021/2/23
 */

@RestController
@RequiredArgsConstructor
@Api(tags = "订单分配管理")
@RequestMapping("/api/order/allot")
@ApiIgnore
public class OrderAllotController {


    private final OrderAllotService orderAllotService;


    @ApiOperation(value = "订单分配列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "1待确认，2已确认，3已发布,4拒绝", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @PostMapping(value = "/list", produces = "application/json; charset=utf-8")
    @Authorization
    public BaseResult allotOrderList(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Fourth.class}) OrderAllotTransportDto para) throws Exception {
        para.setAllotUserId(user.getId());
        BaseResult res = orderAllotService.allotOrderList(para);
        return res;
    }


    @ApiOperation(value = "订单分配货运保存")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId", value = "订单id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "transportUserId", value = "分配运输公司id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "allotNumber", value = "分配数量", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "freight", value = "运费", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "totalFreight", value = "总运费", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @PostMapping(value = "/save", produces = "application/json; charset=utf-8")
    @Authorization
    @NoRepeatSubmit
    @JsonView(ReslutView.aView.class)
    public BaseResult save(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {First.class}) OrderAllotTransportDto para) throws Exception {
        para.setAllotUserId(user.getId());
        BaseResult res = orderAllotService.save(para);
        return res;
    }
    @ApiOperation(value = "订单分配修改")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderAllotTransportId", value = "订单id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "allotNumber", value = "分配数量", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "freight", value = "运费", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "totalFreight", value = "总运费", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @PostMapping(value = "/update", produces = "application/json; charset=utf-8")
    @Authorization
    @JsonView(ReslutView.aView.class)
    public BaseResult update(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Second.class}) OrderAllotTransportDto para) throws Exception {
        para.setAllotUserId(user.getId());
        BaseResult res = orderAllotService.update(para);
        return res;
    }
    @ApiOperation(value = "订单分配删除")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderAllotTransportId", value = "分配id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @PostMapping(value = "/delete", produces = "application/json; charset=utf-8")
    @Authorization
    @JsonView(ReslutView.aView.class)
    public BaseResult delete(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Third.class}) OrderAllotTransportDto para) throws Exception {
        para.setAllotUserId(user.getId());
        BaseResult res = orderAllotService.delete(para.getOrderAllotTransportId());
        return res;
    }





    @ApiOperation(value = "货源订单列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "1待确认，2已确认，3已发布 4拒绝 ", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "当前页从0开始 ", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "size", value = "请求条数 ", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @GetMapping(value = "/transport-list")
    @Authorization
    public BaseResult getOrderListByStatus(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Fourth.class}) PagePara dto) throws Exception {
        dto.setUserId(user.getId());
        BaseResult res = orderAllotService.getOrderListByStatus(dto);
        return res;

    }

    @ApiOperation(value = "货源订单确认/拒绝")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderAllotTransportId", value = "分配id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "status", value = "2已确认,4拒绝", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "remark", value = "备注", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @PostMapping(value = "/updateOrderAllotTransport", produces = "application/json; charset=utf-8")
    @Authorization
    public BaseResult sure(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {Third.class}) TransportOrderDto para) throws Exception {
        para.setUserId(user.getId());
        para.setUserName(user.getName());
        BaseResult res = orderAllotService.updateOrderAllotTransport(para);
        return res;
    }

    @ApiOperation(value = "货源订单详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderAllotTransportId", value = "分配id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "jwtToken", value = "jwtToken", dataType = "string", required = true, paramType = "header")
    })
    @GetMapping(value = "/order-detil")
    @Authorization
    public BaseResult getOrderDetil(@ApiIgnore @CurrUserInfo User user, @ApiIgnore @Validated(value = {First.class}) TransportOrderDto dto) throws Exception {
        dto.setUserId(user.getId());
        BaseResult res = orderAllotService.getOrderDetil(dto.getOrderAllotTransportId());
        return res;

    }


}
